package com.ruoyi.erp.controller;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.erp.domain.bo.SysCodeRuleBo;
import com.ruoyi.erp.domain.vo.SysCodeRuleVo;
import com.ruoyi.erp.service.ISysCodeRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 编码规则Controller
 *
 * @author chenyun
 * @date 2022-03-06
 */
@Validated
@Api(value = "编码规则控制器", tags = {"编码规则管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/sys/codeRule")
public class SysCodeRuleController extends BaseController {

    private final ISysCodeRuleService iSysCodeRuleService;

    /**
     * 查询编码规则列表
     */
    @ApiOperation("查询编码规则列表")
    @PreAuthorize("@ss.hasPermi('sys:codeRule:list')")
    @GetMapping("/list")
    public TableDataInfo<SysCodeRuleVo> list(@Validated(QueryGroup.class) SysCodeRuleBo bo, PageQuery pageQuery) {
        return iSysCodeRuleService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出编码规则列表
     */
    @ApiOperation("导出编码规则列表")
    @PreAuthorize("@ss.hasPermi('sys:codeRule:export')")
    @Log(title = "编码规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated SysCodeRuleBo bo, HttpServletResponse response) {
        List<SysCodeRuleVo> list = iSysCodeRuleService.queryList(bo);
        ExcelUtil.exportExcel(list, "编码规则", SysCodeRuleVo.class, response);
    }

    /**
     * 获取编码规则详细信息
     */
    @ApiOperation("获取编码规则详细信息")
    @PreAuthorize("@ss.hasPermi('sys:codeRule:query')")
    @GetMapping("/{id}")
    public AjaxResult<SysCodeRuleVo> getInfo(@ApiParam("主键")
                                                  @NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return AjaxResult.success(iSysCodeRuleService.queryById(id));
    }

    /**
     * 新增编码规则
     */
    @ApiOperation("新增编码规则")
    @PreAuthorize("@ss.hasPermi('sys:codeRule:add')")
    @Log(title = "编码规则", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody SysCodeRuleBo bo) throws Exception {
        return toAjax(iSysCodeRuleService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改编码规则
     */
    @ApiOperation("修改编码规则")
    @PreAuthorize("@ss.hasPermi('sys:codeRule:edit')")
    @Log(title = "编码规则", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody SysCodeRuleBo bo) throws Exception{
        return toAjax(iSysCodeRuleService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除编码规则
     */
    @ApiOperation("删除编码规则")
    @PreAuthorize("@ss.hasPermi('sys:codeRule:remove')")
    @Log(title = "编码规则" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@ApiParam("主键串")
                                       @NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iSysCodeRuleService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }


    /**
     * 获取编码
     */
    @ApiOperation("获取编码")
    @PreAuthorize("@ss.hasPermi('sys:codeRule:query')")
    @GetMapping("/getCode")
    public AjaxResult<String> getCode(@ApiParam("业务编码")
                                             @NotNull(message = "业务编码不能为空") String bizCode, Date date) {
        List<String> list = iSysCodeRuleService.genCodeByRule(bizCode, date, 1);
        String code = CollUtil.isEmpty(list)?"":list.get(0);
        return AjaxResult.success("",code);
    }

}
